package com.example.monitordata;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    MyObserver myObserver ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 注册
        myObserver= new MyObserver(new Handler());
        Uri uri = Uri.parse("content://cn.itcast.contentobserverdb/info");
        getContentResolver().registerContentObserver(uri,true,myObserver);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销
        getContentResolver().unregisterContentObserver(myObserver);
    }

    class MyObserver extends ContentObserver {

        /**
         * Creates a content observer.
         *
         * @param handler The handler to run {@link #onChange} on, or null if none.
         */
        public MyObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            Log.e(TAG, "onChange: " );
            super.onChange(selfChange);
        }
    }
}
